import './css/index.less'
import Icons from './js/icons'
import Util from './js/util'
import lang from './js/lang'
import initEvent from './js/event'
import initProperty from './js/property'
import initTemplate from './js/template'
import initController from './js/controller'
import initHotkey from './js/hotkey'
import initDanmu from './js/danmu'

class ZPlayer {
    constructor(p) {
        ZPlayer.prototype.__icons = Icons
        ZPlayer.prototype.__util = Util
        ZPlayer.prototype.__event = initEvent()
        this.property = initProperty(this, p)
        this.container = this.property.container
        this.container.classList.add('z-player')
        this.template = initTemplate(this)
        this.video = this.template.video
        this.controller = initController(this)
        this.hotkey = initHotkey(this)
        this.danmu = initDanmu(this)
    }

    play() {
        if (!this.container) throw this.__translate('对象被销毁或发生了未知的意外')
        if (!this.video.paused) return
        const playedPromise = Promise.resolve(this.video.play())
        playedPromise.catch(() => {
            if (!this.video.muted) {
                this.volume(0)
                this.play()
            }
        })
    }

    pause() {
        if (!this.container) throw this.__translate('对象被销毁或发生了未知的意外')
        if (this.video.paused) return
        this.video.pause()
    }

    seek(time) {
        if (!this.container) throw this.__translate('对象被销毁或发生了未知的意外')
        time = Math.max(time, 0)
        if (this.video.duration) {
            time = Math.min(time, this.video.duration)
        }
        this.video.currentTime = time
    }

    volume(v, notice) {
        if (!this.container) throw this.__translate('对象被销毁或发生了未知的意外')
        v = parseFloat(v)
        if (isNaN(v)) return
        v = v.toFixed(2) * 1
        v = Math.max(v, 0)
        v = Math.min(v, 1)
        this.video.volume = v
        if (notice) this.notice(this.__translate('当前音量') + ' : ' + ~~(v * 100))
    }

    notice(text) {
        if (!this.container) throw this.__translate('对象被销毁或发生了未知的意外')
        if (this.noticeTimer) clearTimeout(this.noticeTimer)
        this.template.notice.innerText = text
        this.template.notice.style['top'] = '16px'
        this.noticeTimer = setTimeout(() => {
            this.template.notice.style['top'] = '-20px'
        }, 2000)
    }

    on(name, callback) {
        this.__event.on(name, callback)
    }

    remove(name, callback) {
        this.__event.remove(name, callback)
    }

    destroy() {
        if (!this.container) throw this.__translate('对象被销毁或发生了未知的意外')
        this.pause()
        this.__event.trigger('destroy')
        this.__event = initEvent()
        this.container.classList.remove('z-player')
        this.container.innerHTML = ''
        clearTimeout(this.noticeTimer)
        this.noticeTimer = null
        this.template = null
        this.video = null
        this.controller = null
        this.hotkey = null
        this.danmu = null
        this.container = null
        return true
    }

    __translate(text) { // 翻译
        return lang(text, this.property.lang)
    }
}

export default ZPlayer
